package com.zxzhu.joyread.Presenter;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SignUpCallback;
import com.zxzhu.joyread.Model.RegisterModel;
import com.zxzhu.joyread.Util.PermissionsUtil;
import com.zxzhu.joyread.View.IRegisterView;
import com.zxzhu.joyread.View.RegisterView;

/**
 * Created by zia on 17-7-23.
 */

public class RegisterPresenter implements IRegisterPresenter {

    private final static String TAG = "RegisterPresenterTest";
    private IRegisterView view;
    private RegisterModel model;

    public RegisterPresenter(RegisterView registerView){
        this.view = registerView;
        model = new RegisterModel();
    }

    @Override
    public void register(String username, String password, String bitmapPath) {
        view.showDialog();
        model.registerWithLocalIcon(username, password,null,null,bitmapPath, new SignUpCallback() {
            @Override
            public void done(final AVException e) {
                AVUser.logOut();//此处是为了修复一个bug
                view.getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        view.hideDialog();
                        if(e == null){
                            view.toast("注册成功！");
                            view.getActivity().finish();
                        }
                        else{
                            view.toast("用户名已被注册！");
                        }
                    }
                });
            }
        });

    }

    @Override
    public void sendGetImageIntent(int code) {
        if(PermissionsUtil.hasDiskPermission(view.getActivity(),RegisterView.GET_DISK_PERMISSION)){//如果有权限才执行
            Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            view.getActivity().startActivityForResult(intent,code);
        }
    }

    @Override
    public String getFixedImagePath(Bitmap bitmap) {
        // 图像保存到文件中，并获取路径，等待上传
        return model.saveBitmap(bitmap);
    }

    //请求裁剪照片
    @Override
    public void sendGetFixedIntent(Uri uri,int code) {
        String[] proj = {MediaStore.Images.Media.DATA};
        Cursor cursor = view.getActivity().managedQuery(uri, proj, null, null, null);
        //int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        //imagePath = cursor.getString(column_index);
        Intent intent = new Intent();
        intent.setAction("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");// mUri是已经选择的图片Uri
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);// 裁剪框比例
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 200);// 输出图片大小
        intent.putExtra("outputY", 200);
        intent.putExtra("return-data", true);
        view.getActivity().startActivityForResult(intent, code);
    }

    @Override
    public void setImageView(Bitmap bitmap) {
        view.getImageView().setImageBitmap(bitmap);
    }
}
